//
//  FControl.m
//  test
//
//  Created by 袁飞 on 12-7-12.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "FControl.h"

@implementation FControl
@synthesize name = name_;

-(id)init{
  [super init];

  touch_in_ = false;
  state_ = TOUCH_UP;
  [self setIsTouchEnabled:true];
  //[self setColor:ccc3(0, 0, 255)];
  //[self setOpacity:255];
  [self setContentSize:CGSizeMake(100, 100)];
  
  //[self setAnchorPoint:ccp(0, 0)];

  CGPoint an = [self anchorPoint];
  //NSLog(@"anchor %f, %f", an.x, an.y);
  return self;
}

-(void)drawAnchorPoint{
  CCSprite *sp = [CCSprite spriteWithFile:@"arrow.png"];
  //sp position
  [sp setPosition:[self position]];
  [[self parent] addChild:sp z:10];
}

-(void)registerWithTouchDispatcher{
  [[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:kCCMenuTouchPriority swallowsTouches:false];
}

-(void)ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event{
  if (state_ == TOUCH_UP) {
    return;
  }
  
  //NSLog(@"touch up %@", [self name]);
  double time_end = [NSDate timeIntervalSinceReferenceDate];
  double delta = time_end - time_begin_;
  CGPoint point_end = [self touchLocation:touch];
  //if([self isContainPoint:point_end]){
    [self onTouchUp:point_end];
    //float sx = (point_end.x - point_begin_.x)/delta;
    //float sy = (point_end.y - point_begin_.y)/delta;
    //[self onSlide:point_end Speed:ccp(sx, sy)];
    [self onSlideBegin:point_begin_ End:point_end Time:delta];
  //}
  
  if (!(state_&TOUCH_SLIDE)) {
    //NSLog(@"clicked %@", [self name]);
    [self onClick:point_begin_];
  }
  state_ = TOUCH_UP;
}

-(BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event{
  CGPoint p = [self touchLocation:touch];
  if (![self isContainPoint:p]) {
    return true;
  }
  //NSLog(@"touch down %@", [self name]);
  point_begin_ = [self touchLocation:touch];
  time_begin_ = [NSDate timeIntervalSinceReferenceDate];
  state_ = TOUCH_DOWN;
  [self onTouchDown:point_begin_];
  return true;
}

-(void)ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event{
  if (state_ == TOUCH_UP) {
    return;
  }
  
  //NSLog(@"touch moved %@", [self name]);
  
  CGPoint point = [self touchLocation:touch];
  double time_end = [NSDate timeIntervalSinceReferenceDate];
  double delta = time_end - time_begin_;
  if (![self isContainPoint:point_begin_]) {
    if (state_ == TOUCH_DOWN) {
      [self onTouchCancel:point];
      [self onSlideBegin:point_begin_ End:point Time:delta];
    }
    state_ = TOUCH_UP;
    return;
  }

  state_ |= TOUCH_SLIDE;
  [self onSlideBegin:point_begin_ End:point Time:0];
}

-(void)onTouchDown:(CGPoint)point{
  
}

-(void)onTouchUp:(CGPoint)point{
   
}
-(void)onTouchCancel:(CGPoint)point{

}

-(void)onClick:(CGPoint)point{
  //CCLayer 
}

-(void)onSlideBegin:(CGPoint)begin End:(CGPoint)end Time:(double)t{
   
}

- (CGPoint)touchLocation:(UITouch *)touch{
  CGPoint touchLocation   = [touch locationInView:[touch view]];
  touchLocation           = [[CCDirector sharedDirector] convertToGL:touchLocation];
  touchLocation           = [self convertToNodeSpace:touchLocation];
  return touchLocation;
}

-(CGRect)getControlRect{
  CGSize s = [self contentSize];
  return CGRectMake(0, 0, s.width, s.height);
}

-(CGSize)getControlSize{

}

-(NSString*)getName{
  return @"";
}

-(bool)isContainPoint:(CGPoint)p{
  CGRect rect = [self getControlRect];
  return CGRectContainsPoint(rect, p);
}
@end
